package com.shou.trigger.http;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.DeleteDTO;
import com.shou.api.model.dto.PageQueryDTO;
import com.shou.api.model.dto.UserDTO;
import com.shou.api.model.request.LoginReq;
import com.shou.api.model.response.LoginRes;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.user.model.entity.UserEntity;
import com.shou.domain.user.service.IUserService;
import com.shou.trigger.http.mapstruct.UserDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/user")
public class UserController extends AbstructController{
    @Resource
    private IUserService userService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private UserDTOConvert userDTOConvert;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    public ResultData<LoginRes> login(@RequestBody LoginReq loginReq) {
        // 使用UserService进行用户认证
        UserEntity userEntity = userService.authenticate(loginReq.getUserName(), loginReq.getPassword());
        // 登录成功，创建会话
        StpUtil.login(userEntity.getId());
        // 将用户信息存入Redis
        StpUtil.getSession().set("userName", userEntity.getName());
        return ResultData.success(LoginRes.builder()
                        .id(userEntity.getId())
                        .name(userEntity.getName())
//                        .password(userEntity.getPassword())
                        .phone(userEntity.getPhone())
                        .email(userEntity.getEmail())
                        .role(userEntity.getRole())
                        .createTime(userEntity.getCreateTime())
                        .updateTime(userEntity.getUpdateTime())
                        .token(StpUtil.getTokenValue())
                        .build()
        );
    }
    /**
     * 新增用户信息
     */
    @PostMapping("/register")
    public ResultData<String> register(@RequestBody UserDTO userDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(userDTO);
        // 保存(新增)用户
        userService.insertUser(userEntity);
        return ResultData.success("insertUser success");
    }


    @PostMapping("/reset")
    public ResultData<String> reset(@RequestBody UserDTO userDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(userDTO);
        // 保存(新增)规则
        userService.resetPassWordById(userEntity.getId());
        return ResultData.success("resetPassWord success");
    }
    /**
     * 分页查询
     */
    @GetMapping("/user-info")
    public ResultData<IPage<UserDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<UserEntity> queueMessageList = userService.pageQuery(pageQueryEntity);
        return ResultData.success(userDTOConvert.convertUserEntityPageToDTO(queueMessageList));
    }

    /**
     * 新增用户信息
     */
    @PostMapping("/user-info")
    public ResultData<String> insertUser(@RequestBody UserDTO userDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(userDTO);
        // 保存(新增)规则
        userService.insertUser(userEntity);
        return ResultData.success("insertUser success");
    }

    /**
     * 删除单/多个用户信息
     */
    @DeleteMapping("/user-info")
    public ResultData<String> deleteUserByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除用户信息
        userService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteUserByIds success");
    }

    /**
     * 清空所有用户信息
     */
    @DeleteMapping("/all-user-info")
    public ResultData<String> deleteAll() {
        // 清空用户信息表
        userService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据用户ID更新用户信息
     */
    @PutMapping("/user-info")
    public ResultData<String> updateUserById(@RequestBody @Validated UserDTO user) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        UserEntity userEntity = userDTOConvert.convertUserDTOToEntity(user);
        // 根据ID更新用户信息
        userService.updateUserById(userEntity);
        return ResultData.success("updateUserById success");
    }
}